/**
 * 
 */
package com.agilegroup.crm.ui.fragments;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListFragment;
import com.agilegroup.crm.AgileApplication;
import com.agilegroup.crm.adapters.ViewDetailsAdapter;
import com.agilegroup.crm.model.EntryItem;
import com.agilegroup.crm.model.Item;
import com.agilegroup.crm.model.Lead;
import com.agilegroup.crm.model.SectionItem;
import com.agilegroup.crm.requests.ViewLeadJsonRequest;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

/**
 * @author hisham
 *
 */
public class ViewLeadFragment extends SherlockListFragment{

	private ArrayList<Item> items = new ArrayList<Item>();
	private static final String JSON_CACHE_KEY = "view_lead_json";
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getSherlockActivity().getIntent();
       // getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
        String leadId = intent.getStringExtra("leadId");
        Log.d("leaId", leadId);
        AgileApplication.getInstance().spiceManager.execute(new ViewLeadJsonRequest(leadId),JSON_CACHE_KEY,DurationInMillis.ALWAYS_EXPIRED,new ViewLeadJsonRequestListener());
    }
	
	public class ViewLeadJsonRequestListener implements RequestListener<Lead>{

		public void onRequestFailure(SpiceException spiceException) {
			Toast.makeText(getSherlockActivity(), "Failure", Toast.LENGTH_SHORT).show();
		}
		public void onRequestSuccess(Lead lead) {
			items.add(new SectionItem("Required Information"));
			if(lead.getLeadOwnerName()!=null&&!lead.getLeadOwnerName().equalsIgnoreCase("")){
				items.add(new EntryItem("Lead Owner", lead.getLeadOwnerName()));
			}
			else{
				items.add(new EntryItem("Lead Owner", "  "));
			}
			if(lead.getfName()!=null&&!lead.getfName().equalsIgnoreCase("")){
				items.add(new EntryItem("First Name", lead.getfName()));
			}
			else{
				items.add(new EntryItem("First Name", "  "));
			}
			if(lead.getlName()!=null&&!lead.getlName().equalsIgnoreCase("")){
				items.add(new EntryItem("Last Name", lead.getlName()));
			}
			else{
				items.add(new EntryItem("Last Name", "   "));
			}
			if(lead.getCompanyName()!=null&&!lead.getCompanyName().equalsIgnoreCase("")){
				 items.add(new EntryItem("Company", lead.getCompanyName()));
			}
			else{
				items.add(new EntryItem("Company", "   "));
			}
			if(lead.getLeadStatus()!=null&&!lead.getLeadStatus().equalsIgnoreCase("")){
				if(lead.getLeadStatus().equals("--none--")){
					items.add(new EntryItem("Lead Status", "   "));
				}
				else{
					items.add(new EntryItem("Lead Status", lead.getLeadStatus()));
				}
				
			}
			else{
				items.add(new EntryItem("Lead Status", "   "));
			}
	        
	        items.add(new SectionItem("Lead Information"));
	        if(lead.getTitle()!=null&&!lead.getTitle().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Title", lead.getTitle()));
	        }
	        else{
				items.add(new EntryItem("Title", "   "));
			}
	        if(lead.getPhone()!=null&&!lead.getPhone().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Phone", lead.getPhone()));
	        }
	        else{
				items.add(new EntryItem("Phone", "   "));
			}
	        if(lead.getMobile()!=null&&!lead.getMobile().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Mobile", lead.getMobile()));
	        }
	        else{
				items.add(new EntryItem("Mobile", "   "));
			}
	        if(lead.getEmail()!=null&&!lead.getEmail().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Email", lead.getEmail()));
	        }
	        else{
				items.add(new EntryItem("Email", "   "));
			}
	        if(lead.getRating()!=null&&!lead.getRating().equalsIgnoreCase("")){
	        	if(lead.getRating().equals("--none--")){
	        		items.add(new EntryItem("Rating", "   "));
	        	}
	        	else{
	        		items.add(new EntryItem("Rating", lead.getRating()));
	        	}
	        }
	        else{
				items.add(new EntryItem("Rating", "   "));
			}
	        if(lead.getIndustry()!=null&&!lead.getIndustry().equalsIgnoreCase("")){
	        	if(lead.getIndustry().equals("--none--")){
	        		items.add(new EntryItem("Sector", "   "));
	        	}
	        	else{
	        		items.add(new EntryItem("Sector", lead.getIndustry()));
	        	}
	        }
	        else{
				items.add(new EntryItem("Sector", "   "));
			}
	        
	        items.add(new SectionItem("Address Information"));
	        if(lead.getStreet()!=null&&!lead.getStreet().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Street", lead.getStreet()));
	        }
	        else{
				items.add(new EntryItem("Street", "   "));
			}
	        if(lead.getState()!=null&&!lead.getState().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Area", lead.getState()));
	        }
	        else{
				items.add(new EntryItem("Area", "   "));
			}
	        if(lead.getCityName()!=null&&!lead.getCityName().equalsIgnoreCase("")){
	        	items.add(new EntryItem("City", lead.getCityName()));
	        }
	        else{
				items.add(new EntryItem("City", "   "));
			}
	        if(lead.getCountryName()!=null&&!lead.getCountryName().equalsIgnoreCase("")){
	        	if(lead.getCountryName().equals("--none--")){
	        		items.add(new EntryItem("Country", "   "));
	        	}
	        	else {
	        		items.add(new EntryItem("Country", lead.getCountryName()));
				}
	        }
	        else{
				items.add(new EntryItem("Country", "   "));
			}
	        if(lead.getZipCode()!=null&&!lead.getZipCode().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Zip/Postal Code", lead.getZipCode()));
	        }
	        else{
				items.add(new EntryItem("Zip/Postal Code", "   "));
			}
	        if(lead.getWebSite()!=null&&!lead.getWebSite().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Website", lead.getWebSite()));
	        }
	        else{
				items.add(new EntryItem("Website", "   "));
			}
	        
	        items.add(new SectionItem("Additional Information"));
	        if(lead.getInOutBound()!=null&&!lead.getInOutBound().equalsIgnoreCase("")){
	        	items.add(new EntryItem("In/Out Bound", lead.getInOutBound()));
	        }
	        else{
				items.add(new EntryItem("In/Out Bound", "   "));
			}
	        if(lead.getCarsNum()!=null&&!lead.getCarsNum().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Lead Need's Amount", lead.getCarsNum()));
	        }
	        else{
				items.add(new EntryItem("Lead Need's Amount", "   "));
			}
	        if(lead.getCategory()!=null&&!lead.getCategory().equalsIgnoreCase("")){
	        	 items.add(new EntryItem("Category", lead.getCategory()));
	        }
	        else{
				items.add(new EntryItem("Category", "   "));
			}
	        if(lead.getLeadSource()!=null&&!lead.getLeadSource().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Lead Source", lead.getLeadSource()));
	        }
	        else{
				items.add(new EntryItem("Lead Source", "   "));
			}
	        if(lead.getNumOfEmp()!=null){
	        	items.add(new EntryItem("No. of Employees", lead.getNumOfEmp().toString()));
	        }
	        else{
				items.add(new EntryItem("No. of Employees", "   "));
			}
	        if(lead.getAnnualRevenue()!=null){
	        	items.add(new EntryItem("Annual Revenue", lead.getAnnualRevenue().toString()));
	        }
	        else{
				items.add(new EntryItem("Annual Revenue", "   "));
			}
	        if(lead.getDescription()!=null&&!lead.getDescription().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Description", lead.getDescription()));
	        }
	        else{
				items.add(new EntryItem("Description", "   "));
			}
	        
	        
	        ViewDetailsAdapter adapter = new ViewDetailsAdapter(getSherlockActivity(), items);
	        setListAdapter(adapter);
		}
	 }
  
}
